package com.kaili.zuul.controller;

import com.alibaba.fastjson.JSONObject;
import com.kaili.common.pojo.entity.BaseUser;
import com.kaili.zuul.service.LoginService;
import com.kaili.zuul.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "用户信息", description = "用户信息获取相关API")
public class UserMessageController {

    @Autowired
    private LoginService loginService;

    @GetMapping(value = "/getUserMessage")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public String getUserMessage(){
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JSONObject json = new JSONObject();
//        Map<String,String[]> params = httpServletRequest.getParameterMap();
        String tokenstr = httpServletRequest.getParameter("token");
        BaseUser baseUser = new BaseUser();
        if (tokenstr == null || tokenstr.equals("")){
            json.put("data",baseUser);
            json.put("status",400);
            json.put("message","传入token为空");
            return json.toJSONString();
        }
        Integer token = Integer.valueOf(tokenstr);
        baseUser = loginService.loadUserByUserid(token);

        json.put("data",baseUser);
        json.put("status",200);
        json.put("message","查询成功");
        return json.toJSONString();
    }
}
